import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useCityStore = defineStore('city', () => {
  /*   let city = [...new Set(JSON.parse(localStorage.getItem('cities')))] */
  //从本地存储创建todos响应对象
  const locationCiy = ref(localStorage.getItem('locationCiy') || '')
  const historyCities = ref(JSON.parse(localStorage.getItem('cities') || '[]'))
  // 变更城市
  function changeCities(item) {
    historyCities.value.unshift(item)
    historyCities.value = [...new Set(historyCities.value)]
    // 判断是否重复
    if (historyCities.value.length > 3) {
      historyCities.value.length = 3
    }
    localStorage.setItem('cities', JSON.stringify(historyCities.value))
  }
  return { historyCities, changeCities, locationCiy }
})
